package com.qidi.mvcframework.servlet.webmvc.servlet;

import java.io.File;

public class MyViewResolver {

    // 默认的文件后缀
    private String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootDir;

    public MyViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        templateRootDir = new File(templateRootPath);
    }

    public MyView resolveViewName(String viewName) {
        if (viewName == null || viewName.trim().length() == 0) return null;

        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName : viewName + DEFAULT_TEMPLATE_SUFFIX;

        File templateFile = new File((templateRootDir + File.separator + viewName).replaceAll("/+", "/"));
        return new MyView(templateFile);
    }
}
